package com.socket.netty;


import java.io.UnsupportedEncodingException;
import java.text.NumberFormat;

/**
 * Created by Administrator on 2017/7/4 0004.
 */
public class KeepaliveSocketPackage {

        /*//头标识
        private String headerFlag;
        //数据头
        private String massageHeader;
        //CRC校验码
        private String crcCode;
        //尾标识
        private String endFlag;

        public String getHeaderFlag() {
            return headerFlag;
        }

        public void setHeaderFlag(String headerFlag) {
            this.headerFlag = headerFlag;
        }

        public String getMassageHeader() {
            return massageHeader;
        }

        public void setMassageHeader(String massageHeader) {
            this.massageHeader = massageHeader;
        }

        public String getCrcCode() {
            return crcCode;
        }

        public void setCrcCode(String crcCode) {
            this.crcCode = crcCode;
        }

        public String getEndFlag() {
            return endFlag;
        }

        public void setEndFlag(String endFlag) {
            this.endFlag = endFlag;
        }



        public byte[] getByteStream() throws UnsupportedEncodingException {
            byte[] bodyBytes = this.body.getBytes("gbk");//获得body的字节数组
            int bodyLength = bodyBytes.length;
            int socketLength = 3+bodyLength+8+32;
            byte [] soc = new byte[socketLength];
            //添加校验数据
            int index = 0;
            soc[0]=0x11;
            soc[1]=0x12;
            soc[2]=0x13;
            index+=3;
            //添加8位报文长度（我的博文中也有NumberFormat的用法介绍）
            NumberFormat numberFormat = NumberFormat.getNumberInstance();
            numberFormat.setMinimumIntegerDigits(8);
            numberFormat.setGroupingUsed(false);
            byte [] num = numberFormat.format(socketLength).getBytes();
            for(int i = 0;i<8;i++){
                soc[index++]= num[i];
            }
            //添加body内容
            for(int i = 0;i<bodyLength;i++){
                soc[index++] = bodyBytes[i];
            }
            //添加md5校验码
            byte [] md5Bytes = this.md5.getBytes();
            for (int i = 0; i < num.length; i++) {
                soc[index++] = md5Bytes[i];
            }
            return soc;
        }

        //字节装转报文string
        public String getString(byte [] socketBytes){
            String syncStr = this.bytesToString(socketBytes, 0, 3);
            String socketLength = this.bytesToString(socketBytes, 3, 3+8);
            String body = this.bytesToString(socketBytes, 3+8, socketBytes.length-32);
            String md5 = this.bytesToString(socketBytes,socketBytes.length-32,socketBytes.length);
            return syncStr+socketLength+body+md5;
        }

        //将字节数组转化为string
        public String bytesToString(byte [] bytes,int start,int end){
            String str = "";
            if(bytes.length<end-start){
                return str;
            }
            byte [] bs = new byte[end-start];
            for(int i = 0;i<end-start;i++){
                bs[i] = bytes[start++];
            }
            str = new String(bs);
            return str;
        }

        public String toString(){
            return this.headerFlag+this.massageHeader+this.loginRequest+this.md5;
        }

    }*/
}
